// 用户表单验证
var Check = {

	// 用户名验证
	users : function(obj){

		var users     = (obj === undefined || obj.element === undefined) ? '' : obj.element;
			table     = (obj.table === undefined || obj === undefined) ? 'users' : obj.table;
			url       = (obj === undefined || obj.url === undefined) ? '' :obj.url;
		    innerText = '账号不合法，请检查！',
		    usersReg  = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;

		// users 失去焦点时执行
		return this.styPe({'value':users[0].value, 'regar':usersReg, 'text':innerText, "users":users, "url":url, "table":table});
	},

	// 密码验证
	pass : function(){

		var pass     = document.getElementsByClassName('pass')[0],
		    innerText = '密码不合法，请检查！',
		    passReg  = /^[a-zA-Z]\w{5,17}$/;

		return this.styPe({'value':pass.value, 'regar':passReg, 'text':innerText});
	},

	// 确认密码验证
	passCon : function(){

		var	pass    = document.getElementsByClassName('pass')[0],
		    passCon    = document.getElementsByClassName('passCon')[0],
		    innerText = '密码不一致，请检查！';

		this.pass();
		return this.styPe({'value1':pass.value, 'value2':passCon.value, 'text':innerText});
	},

	// Email 邮箱验证
	email : function(){

		var email    = document.getElementsByClassName('email')[0],
			emailReg  = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
		    innerText = 'Email格式不合法，请检查！';

		return this.styPe({'value':email.value, 'regar':emailReg, 'text':innerText});
	},

	checkEmpty : function(obj){

		if(obj.element[0].value == ''){

			UI.hint({"text":"文本不准为空，重填写！"});
			return false;
		}
	},

	// 验证是否能够提交
	prom : function(){
		
		var num = null; // 初始化
		var users     = document.getElementsByClassName('users');
		num = this.users({'url':'./pose/check_usersAction.php','table':'users','element':users});
		if(num == false){
			return num;
		}
		num = this.pass();
		if(num == false){
			return num;
		}
		num = this.passCon();
		if(num == false){	
			return num;
		}
		num = this.email();
		if(num == false){
			return num;
		}
	},

	// 公共方法
	styPe : function(obj){

		var data = obj.regar != undefined ? obj.value == '' || !obj.regar.test(obj.value) : obj.value1 != obj.value2 || obj.value2 == '';

		if(data){

			UI.hint({"text":obj.text});
			return false;
		}else if(obj.table){

			// ajax请求后端数据处理 
			ajax.post(obj.url, {"value":obj.value, "table":obj.table}, function(res){

				if(res){

					UI.hint({"text":"账号已存在，请更换！"});					
				}
				return false;
			}, 'json');
		}
	}
}